<?php
/**
 * XIE--Scorpion
 *
 * 用户操作
 */
namespace App\Models\Admin;

use DB,Session,Redirect;
use Illuminate\Database\Eloquent\Model;

class advice extends Model
{
	protected $table = 'advices';
    public $timestamps = false;

	/*
     *消息列表   
     *
     * $v 搜索条件
     */
    static function adviceList($v='')
    {
        if($v==''){

            $list=DB::table('advices')->paginate(5);
        }else{

            $list=DB::table('advices')->where('a_text','like',$v)->paginate(5);
        }
        //构建新的数据
        foreach($list as $k=>$v){
            $s_id=$v->s_id;
            $r_id=$v->r_id;
            if($v->s_type=='1'){
                $row1=DB::table('admin_user')->where('admin_id',$s_id)->get();
                $list[$k]->s_name=$row1[0]->admin_user;
            }else{
                $row2=DB::table('user')->where('user_id',$s_id)->get();
                $list[$k]->r_name=$row2[0]->user_name;
            }
            if($v->r_type=='1'){
                $row3=DB::table('admin_user')->where('admin_id',$r_id)->get();
                $list[$k]->s_name=$row3[0]->admin_user;
            }else{
                $row4=DB::table('user')->where('user_id',$r_id)->get();
                $list[$k]->r_name=$row4[0]->user_name;
            }
        }

        return $list;
    }
    
    /*
     *查询特定公司信息
     *
     * $id 公司管理者id
     */
    static function adviceEnterprise($id)
    {
        //接收人是公司
        $list=DB::table('advices')->where('r_type','1')->where('r_id',$id)->paginate(5);
        //
        foreach($list as $k=>$v){
            $s_id=$v->s_id;
            $r_id=$v->r_id;
            if($v->s_type=='1'){
                $row1=DB::table('admin_user')->where('admin_id',$s_id)->get();
                $list[$k]->s_name=$row1[0]->admin_user;
            }else{
                $row2=DB::table('user')->where('user_id',$s_id)->get();
                $list[$k]->r_name=$row2[0]->user_name;
            }
            if($v->r_type=='1'){
                $row3=DB::table('admin_user')->where('admin_id',$r_id)->get();
                $list[$k]->s_name=$row3[0]->admin_user;
            }else{
                $row4=DB::table('user')->where('user_id',$r_id)->get();
                $list[$k]->r_name=$row4[0]->user_name;
            }
        }

        return $list ;
    }


    /*
     *公司回复消息
     *
     * $arr 入库的数据
     */
    static function e_advice($arr,$a='')
    {
        if(DB::table('advices')->insert($arr)){
            if($a!=''){
                DB::table('advices')->where('a_id',$a)->update(array('a_stu'=>1));
            }
            return true;
        }else{
            return false;
        }
    }

}